package org.zjvis.graph.analysis.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import static org.zjvis.graph.analysis.service.constant.GraphConstant.GRAPH_FILE_DIR;

@Service
public class HealthyCheckService {
    private static final Logger logger = LoggerFactory.getLogger(HealthyCheckService.class);

    @Autowired
    private JanusGraphEmbedService janusGraphEmbedService;

    @Autowired
    private GraphAnalysisService graphAnalysisService;

    @Autowired
    private MinioService minioService;

    public boolean check() {
        return checkJanusGraph() && checkMysql() && checkMinio();
    }

    public boolean checkJanusGraph() {
        try {
            return true;
        } catch (Exception e) {
            logger.error("checkJanusGraph fail.", e);
            return false;
        }
    }

    public boolean checkMysql() {
        try {
            String ret = graphAnalysisService.checkVersion();
            logger.info("checkMysql success.");
            return true;
        } catch (Exception e) {
            logger.error("checkMysql fail.", e);
            return false;
        }
    }

    public boolean checkMinio() {
        try {
            minioService.queryOneFile(GRAPH_FILE_DIR, "");
            logger.info("checkMinio success.");
            return true;
        } catch (Exception e) {
            logger.error("checkMinio fail.", e);
            return false;
        }
    }
}
